import { login, register, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  uid: '',
  nickname: '',
  avatar: '',
  roles: '',
  describe: '',
  fans_num: 0, // 粉絲數量
  follow: 0,
  follow_num: 0,
  like_num: 0 // 獲取贊
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_UID: (state, id) => {
    state.uid = id
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_FANSNUM: (state, num) => {
    state.fans_num = num
  },
  SET_FOLLOW: (state, num) => {
    state.follow = num
  },
  SET_FOLLOWNUM: (state, num) => {
    state.follow_num = num
  },
  SET_LIKENUM: (state, num) => {
    state.like_num = num
  },
  SET_DESCRIBE: (state, str) => {
    state.describe = str
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo)
        .then(res => {
          const { data } = res
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  register({ commit }, form) {
    return new Promise((resolve, reject) => {
      register(form)
        .then(res => {
          const { data } = res
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          resolve()
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // get user info
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then(response => {
          const { data } = response
          commit('SET_TOKEN', data.token)
          commit('SET_NICKNAME', data.nickname)
          commit('SET_AVATAR', data.avatar)
          commit('SET_ROLES', data.role)
          commit('SET_UID', data.uid)
          commit('SET_FANSNUM', data.fans_num)
          commit('SET_FOLLOW', data.follow)
          commit('SET_FOLLOWNUM', data.follow_num)
          commit('SET_LIKENUM', data.like_num)
          commit('SET_DESCRIBE', data.describe)
          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // user logout
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      try {
        setTimeout(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', '')
          commit('SET_UID', '')
          removeToken()
          resolve()
        }, 500)
      } catch (error) {
        reject(error)
      }
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', '')
      commit('SET_UID', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
